package buzzword.gui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import buzzword.Client;
import buzzword.ClientFunctions;

/*
 * Dies ist der Hauptframe, dieser Frame ist 800x800 und beherbergt sowohl den Banner als auch
 * das AktionsPanel
 */
public class GuiMainFrame extends JFrame {
	
	
	public static void main(String[] args) {
		new GuiMainFrame();
	}

	private static ClientFunctions clientFunctions;
	private JPanel actionPanel;
	private JPanel contentPanel;
	private JPanel bannerPanel;
	
	static public ClientFunctions getClient() {
		if(clientFunctions == null) {
			clientFunctions = new Client("newPlayer");
		}
		return clientFunctions;
	}
	
	public GuiMainFrame() {
		setTitle("BuzzWord-Bingo");
		setBounds(0, 0, 800, 800);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		setPreferredSize(new Dimension(800, 800));
		setLayout(null);
		setContentPane(getContentPanel());
		switchActionPanel(new MainMenuPanel());
		repaint();
		setResizable(false);
		this.setVisible(true);
		pack();
	}
	
	public void switchActionPanel(JPanel newPanel) {
		getActionPanel().removeAll();
		newPanel.setBounds(400-(newPanel.getWidth()/2), 300-(newPanel.getHeight()/2), newPanel.getWidth(), newPanel.getHeight());
		newPanel.setOpaque(false);
		getActionPanel().add(newPanel);
		SwingUtilities.updateComponentTreeUI(this);
		repaint();
	}
	
	
	private JPanel getActionPanel() {
		if(actionPanel == null) {
			actionPanel = new JPanel() {
				public void paintComponent(Graphics g) {
					try {
						g.drawImage(ImageIO.read(new File("Graphics/background.gif")), 0, 0, null);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			};
			actionPanel.setBounds(0,200,800,600);
			actionPanel.setLayout(null);
		}
		return actionPanel;
	}
	
	private JPanel getContentPanel() {
		if(contentPanel == null) {
			contentPanel = new JPanel() {
				public void paintComponent(Graphics g) {

					try {
						g.drawImage(ImageIO
								.read(new File("Graphics/banner.gif")), 0, 0,
								null);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			};
			contentPanel.setSize(800, 800);
			contentPanel.setLayout(null);
			contentPanel.add(getActionPanel());
			
		}
		return contentPanel;
	}
	
}
